package com.liesheng.haylou.ui.device.vm;

import android.bluetooth.BluetoothDevice;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.liesheng.haylou.R;
import com.liesheng.haylou.base.BaseVm;
import com.liesheng.haylou.bluetooth.BleDataInfo;
import com.liesheng.haylou.common.recycleView.CommonAdapter;
import com.liesheng.haylou.databinding.ActivitySelectDeviceBinding;
import com.liesheng.haylou.databinding.ItemSelectDeviceBinding;
import com.liesheng.haylou.db.entity.BoundedDevice;
import com.liesheng.haylou.ui.device.DeviceDetailActivity;
import com.liesheng.haylou.ui.device.SelectDeviceActivity;
import com.liesheng.haylou.ui.main.MainActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Date:2019/12/4
 * <p>
 * Author:wuzhiguang
 * <p>
 * Description:
 * 选择设备
 */
public class SelectDeviceVm extends BaseVm<SelectDeviceActivity> {

    ActivitySelectDeviceBinding mBinding;

    private List<BleDataInfo> datas = new ArrayList<>();

    public SelectDeviceVm(SelectDeviceActivity mActivity) {
        super(mActivity);
        mBinding = mActivity.mBinding;
    }


    public void init() {
        GridLayoutManager layoutManager = new GridLayoutManager(mActivity, 2);
        mBinding.rvSlect.setLayoutManager(layoutManager);
        mBinding.rvSlect.setAdapter(adapter);
    }

    public void addData(BleDataInfo device) {
        datas.add(device);
        adapter.notifyDataSetChanged();
    }


    CommonAdapter adapter = new CommonAdapter<BleDataInfo, ItemSelectDeviceBinding>(mActivity, datas, R.layout.item_select_device) {

        @Override
        public void contentConvert(ItemSelectDeviceBinding binding, final BleDataInfo bleDataInfo, int position) {
            final BluetoothDevice device = bleDataInfo.getDev();
            binding.tvBlueName.setText(device.getName() + "(" + device.getAddress() + ")");
            //选中设备
            binding.getRoot().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    BoundedDevice bd = new BoundedDevice();
                    bd.setAddress(bleDataInfo.getrLaddress());
                    bd.setName(device.getName());
                    bd.setType(0);
                    bd.setBroadcast_count(bleDataInfo.getAdCount());

                    MainActivity.startBy(mActivity);//先清除掉中间已经开启的activity
                    DeviceDetailActivity.startBy(mActivity, bd,device);
                    mActivity.finish();
                }
            });
        }
    };
}
